package com.imooc.demo.service.impl;

import com.imooc.demo.dao.AdminMapper;
import com.imooc.demo.entity.Admin;
import com.imooc.demo.entity.Result;
import com.imooc.demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Result login(Admin admin) {
        if (admin == null || StringUtils.isEmpty(admin.getUsername()) || StringUtils.isEmpty(admin.getPassword())) {
            return new Result(false, "用户名或密码不能为空");
        }
        Admin admin1 = adminMapper.selectAdminByUsername(admin.getUsername());
        if (admin1 != null && admin1.getPassword().equals(admin.getPassword())) {
            return new Result(true, "登录成功", admin1);
        }
        return new Result(false, "登录失败:用户名或密码错误", admin);
    }

    @Override
    public Admin getAdminById(String id) {
        if (!StringUtils.isEmpty(id)) {
            Admin admin = adminMapper.selectByPrimaryKey(id);
            return admin;
        }
        return null;
    }
}
